#include <iostream>

#define MASK a + c + d

template<typename T>
std::string
binary(T num)
{
  std::string tmp = "";
  std::size_t one = 1;
  for (int i = 0; i < sizeof(T) * 8; ++i)
    {
      if (num & (one << i))
	{
	  tmp = "1" + tmp;
	}
      else
	{
	  tmp = "0" + tmp;
	}

      if ((i+1) % 8 == 0)
	{
	  tmp = " " + tmp;
	}
    }
  
  return tmp;
}

int main()
{
  // turn on n bits from position i to position j, where j-i+1 = n
  std::size_t i = 5;
  std::size_t j = 10;
  std::size_t num = 0;
  std::size_t one = 1;
  std::size_t mask = one << (j-i+1);
  mask--;

  std::cout << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  mask = mask << i;

  std::cout << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  num |= (std::size_t)1 << 8;
  
  std::cout << num << std::endl << "\t" << binary<std::size_t>(num) << std::endl;

  num |= (std::size_t)1 << 10;
  
  std::cout << num << std::endl << "\t" << binary<std::size_t>(num) << std::endl;


  /*
  mask = 0;

  std::cout << "initialization " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;
  
  std::cout << "sizeof(size_t) = " << sizeof(std::size_t) << std::endl;

  std::size_t pos = sizeof(std::size_t) * 8 - 1;

  // turn on msb and its neighbor
  mask = mask | (1 << pos--);
  mask = mask | (1 << pos);
  
  std::cout << "2 last bits on: " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;
  
  mask = ~mask;
  std::cout << "negated:        " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  mask = 0;
  mask = mask | (1 << pos--);
  mask = mask | (1 << pos);
  pos = sizeof(std::size_t) * 8 - 1;

  // turn on msb and its neighbor
  mask = mask | (1 << pos--);
  mask = mask | (1 << pos);

  mask = mask >> 30;
  std::cout << "shift right 30 units: " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  mask = 0;
  std::size_t m = (sizeof(std::size_t) * 8 - 2) / 2;
  for (std::size_t i = 0; i < m; ++i)
    {
      mask |= (1 << i);
    }
  std::cout << "value mask on: " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  mask <<= 15;
  std::cout << "shift right 15 units: " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;

  mask = 1 << 31;
  std::cout << "last bit on: " << mask << std::endl << "\t" << binary<std::size_t>(mask) << std::endl;*/
}
